<?php
$objXajax->registerFunction("cusfrmdailyreport_load");
$objXajax->registerFunction("cusfrmdailyreport_productDetail");
$objXajax->registerFunction("cusfrmdailyreport_loadservice");

function cusfrmdailyreport_loadservice($params=''){ 
    $objResponse = new xajaxResponse();// dong luon luon co khi goi ham ajax
    $cbbchk = createOptionByService(1,'cusfrmdailyreport_chkservice','cusfrmdailyreport',true) ;
    $objResponse->addAssign('cusfrmdailyreport_tdservice', 'innerHTML', $cbbchk);                 
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax   
}

function cusfrmdailyreport_load($params=''){ 
    global $lang,$path;
  
    $objResponse = new xajaxResponse();// dong luon luon co khi goi ham ajax
    $addPath = '_'.$reporttype;
//    $dateFrom = $dateFrom==''?SYSTEM_DATE_START  : $dateFrom;
//    $dateTo = $dateTo==''?SYSTEM_DATE  : $dateTo;
    $datasum = array();
    $arrData = cusfrmdailyreport_createArray($datasum,$params);     
    $oTbs = new clsTinyButStrong();
    $oTbs->LoadTemplate('templates/'.$lang.'/'.$path.'/cus_dailyreport_grid1.html');
    $oTbs->MergeBlock('blk_data', $arrData); 
    $oTbs->MergeBlock('blk_data_sum', $datasum); 
    $oTbs->Show(TBS_NOTHING);  
    $objResponse->addAssign('cusfrmdailyreport_divGrid', 'innerHTML', $oTbs->Source);
    $objResponse->addScriptCall("dataTableObj.CreatDataTable('cusfrmdailyreport_tbl');");  
    $objResponse->addScript('changeWaiting(1);');                      
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax   
}

function cusfrmdailyreport_createArray(&$datasum,$params){
    global $objDbUpdate,$tbl_postfix;
     //  echo '<pre'; print_r($params);die();
     $service =   $params['service'];    
     $fromdate =   $params['fromdate'];    
     $todate =   $params['todate'];    
     $reporttype =   $params['reporttype']; 
     $arrService = explode(',',substr($service,1));   
    $groupby = " group by DATE_FORMAT(h.timeend,'%Y-%m-%d') order by  DATE_FORMAT(h.timeend,'%Y-%m-%d') ";
    if($reporttype==2){
     $groupby = " group by h.room_id order by h.room_id ";
    }
    elseif($reporttype==3){
      $groupby = ' group by h.user_update order by h.user_update ' ; 
    }
    
    $sql = " select r.name as roomname,DATE_FORMAT(h.timeend,'%Y-%m-%d') as timeend,r.id as room_id, h.generate_id,
     u.username as username,sum(h.price) as price,sum(h.totalprice) as totalprice   
     from tbl_history_book_room$tbl_postfix as h join tbl_room$tbl_postfix r ON h.room_id = r.id join tbl_user as u ON u.id = h.user_update 
     where datediff('$dateFrom',h.timeend)<=0 and datediff('$dateTo',h.timeend)>=0 $groupby ";//
     $arr = $objDbUpdate->GetArray($sql);
     $count = count($arr);
     $totalsum = 0;
     $pricesum = 0;
     for($i=0;$i<$count;$i++){
          $arr[$i]['productprice']= $arr[$i]['totalprice'] - $arr[$i]['price'] ;      
          $totalsum  += (int)$arr[$i]['totalprice'] ;
          $pricesum  += (int)$arr[$i]['price'] ;
     } 
         
    $datasum[0]['totalprice']= $totalsum;
    $datasum[0]['price']= $pricesum;
    $datasum[0]['productprice']= $totalsum-$pricesum;
    $datasum = array();
    return  $arr  ;
}

function createSQLByService($serviceId,$reporttype,$frmdate,$todate){
    global $hotel_id;
    $lang = $_SESSION['lang'];
    $dateFormat = '%d/%m/%Y' ;
    if($lang=='en'){
     $dateFormat = '%Y-%m-%d' ;    
    }
    $table =''; // bang can lay.
    $total ='total'; // field can lay.
    switch($serviceId){
        case 1: $table='ht_current_book_'.$hotel_id;break;   
        case 2: $table='cf_current_product_'.$hotel_id ;
                $total = 'price*num';break;    //truong hop service type la   ca fe thi truong total la tich cua so luong va gia san pham
        case 3: $table='bi_current_book_'.$hotel_id ;break;
        case 4: $table='ka_current_book_'.$hotel_id ;break;
        case 5: $table='ms_current_book_'.$hotel_id ;break;
        case 6: $table='fm_current_book_'.$hotel_id ;break;
    }
     switch($reporttype){
        case 1: return "UNION ALL SELECT $total as total , DATE_FORMAT(date_created,'$dateFormat') as frmdate from $table" ;break;
        case 2: return "UNION ALL SELECT $total as total , DATE_FORMAT(date_created,'$dateFormat') as frmdate,
        DATE_FORMAT(date_created,'%v') as wy,DATE_FORMAT(date_created,'%w') as wm from $table";break;
        case 3: return ;break;
        case 4: return ;break;        
    }
    
} 

function cusfrmdailyreport_productDetail($generate_id){
    global $objDbUpdate,$lang,$tbl_postfix,$path;
    $objResponse = new xajaxResponse();// dong luon luon co khi goi ham ajax
    $sql = " select name as productname,num as num,price as price from tbl_history_product$tbl_postfix where generate_id = '$generate_id'  ";//
   // echo $sql; die();
     $arr = $objDbUpdate->GetArray($sql);
     $count = count($arr);
     $pricesum = 0;
     for($i=0;$i<$count;$i++){
          $pricesum  += (int)$arr[$i]['price'] ;
     }
     $datasum[0]['price']= $pricesum;
    $oTbs = new clsTinyButStrong();
    $oTbs->LoadTemplate('templates/'.$lang.'/'.$path.'/hotel_reportgeneral_productdetail_grid.html');
    $oTbs->MergeBlock('blk_data', $arr); 
    $oTbs->MergeBlock('blk_data_sum', $datasum); 
    $oTbs->Show(TBS_NOTHING);  
    $objResponse->addAssign('cusfrmdailyreport_productDetail', 'innerHTML', $oTbs->Source);
    $objResponse->addScript("$('#cusfrmdailyreport_productDetail').dialog('open');");
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax             
    
} 

# end region process data
?>
